<?php

namespace App\Validators\Clbum;

use App\Validators\PassesValidator;
use Illuminate\Support\Facades\Input;
use \Prettus\Validator\Contracts\ValidatorInterface;
use \Prettus\Validator\LaravelValidator;

class GradeValidator extends LaravelValidator
{
    use PassesValidator;
    protected $rulesMessages=[
        'name.required' => '年纪名称是必填的',
        'unique.required' => '年纪名称已经存在',
        'entrance_date.required' => '入学时间是必填的',
        'graduate_date.required'  => '毕业时间是必填的',
        'clbum_cnt.required'  => '班级数量是必填的',
        'clbum_cnt.integer'  => '班级数量格式错误',
        'clbum_cnt.min'  => '班级数量数量错误',
        ];

    protected $rules = [
        ValidatorInterface::RULE_CREATE => [
            'name'=>'required|unique:grade',
            'entrance_date'=>'required',
            'graduate_date'=>'required',
            'clbum_cnt'=>'required|integer|min:1',
        ],
        ValidatorInterface::RULE_UPDATE => [
            'name'=>'required|unique:grade,name,{id}',
            'entrance_date'=>'required',
            'graduate_date'=>'required',
            'clbum_cnt'=>'required',
        ],
   ];

    public function callBackGetRules(array $rules){
        $result=[];
        $value_change = array('{id}'=>Input::get('id'));
        foreach ($rules as $key=>$value){
            if(is_string($value) && str_contains($value,'{id}')){
                $result[$key]=strtr($value,$value_change);
            }else{
                $result[$key]=$value;
            }
        }
        return $result;
    }
}
